package com.pd.backend.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pd.backend.dtos.PagePdCoreStationDto;
import com.pd.backend.dtos.PageResponseResult;
import com.pd.backend.dtos.PdCoreStationDto;
import com.pd.backend.dtos.ResponseResult;
import com.pd.backend.entity.PdCoreStation;
import com.pd.backend.enums.AppHttpCodeEnum;
import com.pd.backend.mapper.PdCoreStationMapper;
import com.pd.backend.service.PdCoreStationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 岗位业务层实现类
 * @Auther : 菜鸡编程
 * @Date:2023/1/13
 * @Description:
 * @VERSON:1.8
 */
@Service
@Slf4j
@Transactional
public class PdCoreStationServiceImpl implements PdCoreStationService {

    @Autowired
    private PdCoreStationMapper pdCoreStationMapper;
    /**
     * 岗位管理:分页模糊查询
     * @param pagePdCoreStationDto
     * @return
     */
    @Override
    public ResponseResult PagePdCoreStation(PagePdCoreStationDto pagePdCoreStationDto) {
        //校验参数
        if (Objects.isNull(pagePdCoreStationDto)) {
            //请求参数为空,返回错误消息:无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer page = pagePdCoreStationDto.getPage();
        Integer size = pagePdCoreStationDto.getSize();
        String name = pagePdCoreStationDto.getName();
        Long orgId = pagePdCoreStationDto.getOrgId();
        Date createTime = pagePdCoreStationDto.getStartTime();
        Date endTime = pagePdCoreStationDto.getEndTime();

        //开始分页
        PageHelper.startPage(page,size);

        //调用接口获取岗位数据
        List<PdCoreStation> pdCoreStationList =  pdCoreStationMapper.PagePdCoreStation(name,orgId,createTime,endTime);
        if (Objects.isNull(pdCoreStationList)) {
            //数据为空,返回错误消息:数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //获取分页后的所有数据
        PageInfo<PdCoreStation> pdCoreStationPageInfo = new PageInfo<>(pdCoreStationList);

        //返回数据
        return ResponseResult.okResult(pdCoreStationPageInfo);
    }

    /**
     * 新增岗位
     * @return
     */
    @Override
    public ResponseResult addPdCoreStation(PdCoreStationDto pdCoreStationDto) {
        //校验参数
        if (Objects.isNull(pdCoreStationDto)) {
            //参数为空,返回错误消息:缺少参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        String name = pdCoreStationDto.getName();
        Integer status = pdCoreStationDto.getStatus();
        //岗位名称以及状态不能为空
        if (StringUtils.isBlank(name)) {
            //岗位名称为空,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        if (Objects.isNull(status)) {
            //状态为空,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        //调用接口,新增岗位
        boolean result = pdCoreStationMapper.addPdCoreStation(pdCoreStationDto);
        if (!result) {
            //新增失败,返回错误消息:新增失败
            return ResponseResult.errorResult(AppHttpCodeEnum.DATE_INSERT_ERROR);
        }
        //新增成功
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id修改岗位
     * @param
     * @return
     */
    @Override
    public ResponseResult updateById(PdCoreStation pdCoreStation) {
        //校验参数
        if (Objects.isNull(pdCoreStation)) {
            //参数为空,返回错误消息:无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //处理业务
        String name = pdCoreStation.getName();
        Integer status = pdCoreStation.getStatus();
        //岗位名称以及状态不能为空
        if (StringUtils.isBlank(name)) {
            //岗位名称为空,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        if (Objects.isNull(status)) {
            //状态为空,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        //调用接口
        boolean result = pdCoreStationMapper.updateById(pdCoreStation);
        if (!result) {
            //修改失败,返回错误消息:修改失败
            return ResponseResult.errorResult(AppHttpCodeEnum.DATE_UPDATE_ERROR);
        }
        //修改成功
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id批量删除岗位
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteByIds(Long[] ids) {
        //校验参数(无需校验,必定存在)
        for (Long id : ids) {
            //调用接口 根据id查询岗位
            PdCoreStation pdCoreStation = pdCoreStationMapper.selectById(id);

            if (Objects.isNull(pdCoreStation)) {
                //数据为空,返回错误消息:数据不存在
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            //判断岗位状态是否启用
            if (pdCoreStation.getStatus()==1) {
                //岗位状态启用中,返回错误消息:请修改状态后再删除
                return ResponseResult.errorResult(AppHttpCodeEnum.DATE_STATUS_EX);
            }
            //调用接口根据id删除岗位
            boolean result = pdCoreStationMapper.deleteById(id);
            if (!result) {
                //删除失败,返回错误消息:删除失败
                return ResponseResult.errorResult(AppHttpCodeEnum.DATE_DELETE_ERROR);
            }
        }
        //删除成功
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 根据组织id查询所有岗位
     * @param orgId
     * @return
     */
    @Override
    public ResponseResult listByOrgId(Long orgId) {
        //校验参数
        if (Objects.isNull(orgId)) {
            //参数为空,返回错误消息:缺少参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //调用接口 根据组织id查询所有岗位
        List<PdCoreStation> pdCoreStationList = pdCoreStationMapper.listByOrgId(orgId);

        if (Objects.isNull(pdCoreStationList)) {
            //查询结果为空,返回错误消息:数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //返回数据
        return ResponseResult.okResult(pdCoreStationList);
    }
}
